package com.atguigu.gulimall.order.config;

import com.zaxxer.hikari.HikariDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

/**
 * @Author 小坏
 * @Date 2021/2/13 17:59
 * @Version 1.0
 * @program: 父工程 gulimall 万物起源之地
 */

@Configuration
public class MySeataConfig {

    @Autowired
     DataSourceProperties dataSourceProperties;

    @Bean
    public DataSource dataSource(DataSourceProperties dataSourceProperties) {
        //用这个代理把我们原来的数据源包装、包装以后我们的自定义的数据源就放进去了
        //这个服务这样定义数据源、其他服务都一样
        HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder ().type (HikariDataSource.class).build ();
        if (StringUtils.hasLength (dataSourceProperties.getName ())) {
            dataSource.setPoolName (dataSourceProperties.getName ());
        }
        return new DataSourceProxy (dataSource);
    }
}
